package com.longjianghuasheng.logistics.business.freight

import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import com.longjianghuasheng.logistics.base.BaseMultiStateViewModel
import com.longjianghuasheng.logistics.data.model.bean.ApiResponseNoData
import com.longjianghuasheng.logistics.entity.CarInfo
import com.longjianghuasheng.logistics.entity.Company
import com.longjianghuasheng.logistics.ext.*
import com.longjianghuasheng.logistics.network.apiService
import com.longjianghuasheng.logistics.repository.request.HttpRequestCoroutine

class DriverCooperationViewModel : BaseMultiStateViewModel() {

    var names = ""
    var mobile = ""
    var code = ""
    var card_front = ""
    var card_back = ""
    var books = ""
    var team_id = 0
    val cars_info by lazy { mutableListOf<CarInfo>() }


    val startCount by lazy {
        ObservableInt()
    }


    val notify by lazy {
        MutableLiveData<ApiResponseNoData>()
    }

    fun onGetCode() {

        if (!checkPhone(mobile)) return
        commit({ apiService.get_code(mobile, 2) }, {
            startCount.set(60)
        })
    }


    fun driver_work() {
        var isSuccess = false
        when {

            names.judge("请输入姓名") -> {}

            !checkPhone(mobile) -> {}
            code.isBlank() -> toastNormal("验证码不能为空")
            card_front.judge("请上传驾驶证正面") -> {}
            card_back.judge("请上传驾驶证背面") -> {}
            books.judge("请上传从业资格证") -> {}
            cars_info.any { it.cars_type.isBlank() } -> toastNormal("请填写车型")
            cars_info.any { it.weights.isBlank() } -> toastNormal("请填写载重")
            cars_info.any { it.lengths.isBlank() } -> toastNormal("请填写车长")
            cars_info.any { it.license.isBlank() } -> toastNormal("请填写车牌照")
            cars_info.any { it.books.isBlank() } -> toastNormal("请上传行驶证")

            else -> isSuccess = true
        }

        if (!isSuccess) return


        commit2({
            HttpRequestCoroutine.driver_work(
                team_id,
                names,
                mobile,
                code,
                card_front,
                card_back,
                books,
                cars_info.toTypedArray(),

                )
        }, notify)


    }

    val result by lazy {
        MutableLiveData<ArrayList<Company>>()
    }

    fun team_work_list() {
        commit({ apiService.team_work_list() }, result)
    }
}